What is Pandas
-
Is is an open-source Python library used for data manipulation and
analysis. it can analyze csv data and perform operations as mean, min,
max etc.
parsing multiple file formats converting input data table into a NumPy matrix arrays.
Datastructures in Pandas
-
Pandas provides two types of classes for handling data: Series,
Dataframes
1. Series
A one-dimensional labeled array holding data of any type such as integers, strings, Python objects etc.
| Internally | Examples |
|---|---|
Series is composed of a data array and an index array. They are managed as two separate, parallel arrays. The Index Array: This is an object that handles labels, lookups, and alignment. The Values Array: If all data is the same type (e.g., all integers), this is a contiguous, high-performance block of memory (like a C array). When you mix types, it becomes an array of references to Python objects. |
|
2. Dataframes
-
A 2-dimensional data structure that holds data like a two-dimension
array or a table with rows and columns.(Eg: csv file)
df = pd.DataFrame() #Empty DataFrame
print(df)
Columns: []
Index: []
df = pd.DataFrame([5, 6])
print(df)
0
0 5
1 6
data = {
'Age': [20, 21, 19],
'Score': [85, 92, 78]
}
df = pd.DataFrame(data, index=['Alice', 'Bob', 'Charlie'])
print (df)
Age Score //Output
Alice 20 85
Bob 21 92
Charlie 19 78
df = pd.DataFrame({'c1': [1, 2], 'c2': [3, 4]}, //taking dictionary as argument
index=['r1', 'r2'])
print(df) # c1 c2
#r1 1 3
#r2 2 4
// Append additional rows
df = pd.DataFrame([[5, 6], [1.2, 3]])
print(df) # 0 1
# 0 5.0 6
# 1 1.2 3
r = pd.Series([0, 0], name='r3')
df = df.append(r)
print('{}\n'.format(r)) # 0 1
# 0 5.0 6
# 1 1.2 3
# r3 0.0 0
// Dropping rows, coloumns
df = pd.DataFrame({'c1': [1, 2], 'c2': [3, 4], 'c3': [5, 6]}, index=['r1', 'r2'])
print(df) # c1 c2 c3
# r1 1 3 5
# r2 2 4 6
df = df.drop(labels='r1')
print(df) # c1 c2 c3
# r2 2 4 6